package top.misec.push.impl;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import top.misec.push.AbstractPush;
import top.misec.push.model.PushMetaInfo;

@Log4j2
public class BarkPush extends AbstractPush {

    @Override
    protected String generatePushUrl(PushMetaInfo metaInfo) {
        return metaInfo.getToken();
    }

    @Override
    protected String generatePushBody(PushMetaInfo metaInfo, String content) {
        return new Gson().toJson(new MessageModel(content));
    }

    @Override
    protected boolean checkPushStatus(JsonObject jsonObject) {
        if (null == jsonObject) {
            return false;
        }
        JsonElement code = jsonObject.get("code");
        if (code == null) {
            return false;
        }
        return code.getAsInt() == 200;
    }

    @Getter
    static class MessageModel {
        private final String group = "bili";
        private final String title = "BILIBILI-HELPER任务简报";
        private final String icon = "https://www.bilibili.com/favicon.ico";
        private final String body;

        public MessageModel(String content) {
            this.body = content.replaceAll("\r\n\r", "");
        }
    }

}
